package net.oschina.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.MApp;
import net.oschina.app.R;
import net.oschina.app.bean.Event;
import net.oschina.app.util.PreferencesUtils;

import java.util.List;

/**
 * Created by huang on 2017-4-5.
 *近期活动Adapter
 */

public class RecentEventsAdapter extends BaseRecyclerViewAdapter{
    private List<Event> datas;
    public RecentEventsAdapter(Context context, List<Event> datas) {
        super(context);
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MApp.mContext).inflate(R.layout.events_rv_item,parent,false);
        RecentEventViewHolder viewHolder = new RecentEventViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        RecentEventViewHolder viewHolder = (RecentEventViewHolder) holder;
        Event event = datas.get(position);
        int id = event.getId();
        //初始化条目点击状态
        initItemState(viewHolder, id);

        //设置item的值
        Glide.with(MApp.mContext).load(event.getCover()).fitCenter().into(viewHolder.imgEventsCover);//加载图片

        viewHolder.tvEventTitle.setText(event.getTitle());
        viewHolder.tvEventsSpot.setText(event.getSpot());
        viewHolder.tvEventsStartTime.setText(event.getStartTime());
        viewHolder.imgEventsStatusChecked.setVisibility(View.GONE);

    }

    //初始化条目点击状态
    private void initItemState(RecentEventViewHolder viewHolder, int id) {
        String ids = PreferencesUtils.getString(MApp.mContext, "reItem_isRead", "");
        if(ids.contains(id+"")){
            viewHolder.tvEventTitle.setTextColor(Color.parseColor("#9C9A9C"));
        } else {
            viewHolder.tvEventTitle.setTextColor(Color.parseColor("#5A5D5A"));
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    class RecentEventViewHolder extends XRecyclerView.ViewHolder {
        ImageView imgEventsStatusChecked;
        ImageView imgEventsCover ;
        TextView tvEventTitle;
        TextView tvEventsStartTime;
        TextView tvEventsSpot;
        public RecentEventViewHolder(View itemView) {
            super(itemView);
            imgEventsCover = (ImageView) itemView.findViewById(R.id.img_events_cover);
            tvEventTitle = (TextView) itemView.findViewById(R.id.tv_events_title);
            tvEventsStartTime = (TextView) itemView.findViewById(R.id.tv_events_startTime);
            tvEventsSpot = (TextView) itemView.findViewById(R.id.tv_events_spot);
            imgEventsStatusChecked = (ImageView) itemView.findViewById(R.id.img_events_status_checked);
        }
    }
}
